package com.mx.recycle_adapt.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.mx.adapt.anytype.MXBaseTypeAdapt
import com.mx.adapt.anytype.MXEmptyLine
import com.mx.adapt.anytype.MXLayoutManager
import com.mx.recycle_adapt.R
import com.mx.recycle_adapt.databinding.AdaptSimpleImgBinding
import kotlinx.android.synthetic.main.activity_any_type_recycle_view.*

class AnyTypeActivity : AppCompatActivity() {
    private val typeLayoutManager by lazy { MXLayoutManager(this) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_any_type_recycle_view)
        typeLayoutManager.setAdapts(createAdapts())
        typeLayoutManager.attachToRecycleView(recycleView)
    }

    private fun createAdapts(): List<MXBaseTypeAdapt<*>> {
        val adapts = arrayListOf<MXBaseTypeAdapt<*>>()
        adapts.addAll(
            arrayOf(
                SpanCountAdapt(2),
                MXEmptyLine(),
                SpanCountAdapt(3),
                MXEmptyLine(),
                SpanCountAdapt(4),
                MXEmptyLine(),
                SpanCountAdapt(2),
                MXEmptyLine(),
                SpanCountAdapt(1),
                MXEmptyLine(),
                SpanCountAdapt(3),
                MXEmptyLine(),
                SpanCountAdapt(4),
                MXEmptyLine(),
                SpanCountAdapt(1),
                MXEmptyLine(),
                SpanCountAdapt(3),
                MXEmptyLine(),
                SpanCountAdapt(1),
                MXEmptyLine(),
                SpanCountAdapt(4),
                MXEmptyLine(),
                SpanCountAdapt(2),
                MXEmptyLine(),
                SpanCountAdapt(3)
            )
        )
        return adapts
    }

    private class SpanCountAdapt(val count: Int) : MXBaseTypeAdapt<Int>(
        arrayListOf(
            R.mipmap.item1,
            R.mipmap.item2,
            R.mipmap.item3,
            R.mipmap.item4,
            R.mipmap.item5,
            R.mipmap.item6,
            R.mipmap.item1,
            R.mipmap.item2,
            R.mipmap.item3,
            R.mipmap.item4,
            R.mipmap.item5,
            R.mipmap.item6,
        )
    ) {
        init {
            setItemClick { position, record ->
                list.removeAt(position)
                notifyItemRemoved(position)
            }
        }

        override fun createItem(
            inflater: LayoutInflater,
            parent: ViewGroup
        ): ViewBinding {
            return AdaptSimpleImgBinding.inflate(inflater, parent, false)
        }

        override fun getSpanSize(): Int {
            return count
        }

        override fun bindView(position: Int, binding: ViewBinding, record: Int) {
            binding as AdaptSimpleImgBinding
            binding.image.setImageResource(record)
        }

    }
}